package org.example.myframework.frame.demo.designPattern.filterPattern;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 过滤器工厂
 */
public class ArticleFilterFactory {

    /**
     * 获取过滤器列表
     * @param articleFilterEnums 需要执行的过滤器逻辑
     * @return
     */
    public static List<IArticleFilter> createArticleFilter(List<ArticleFilterEnum> articleFilterEnums) {
        List<IArticleFilter> articleFilterList = new ArrayList<>();
        if (CollectionUtils.isEmpty(articleFilterEnums)) {
            articleFilterEnums.stream()
                    .map(s ->
                         ArticleFilterFactory.createFilterByEnum(s)
                    ).collect(Collectors.toList());
        }
        return articleFilterList;
    }

    /**
     * 根据枚举类型获取过滤器对象
     * @param filterEnum 枚举
     * @return
     */
    public static IArticleFilter createFilterByEnum(ArticleFilterEnum filterEnum) {
        IArticleFilter iArticleFilter = null;

        switch (filterEnum) {
            // 枚举可以用作key值
            case WORD_COUNT:
                iArticleFilter = new WordCountArticleFilter();
                break;
            default:
                break;
        }
        return iArticleFilter;
    }


}
